package com.yuwenchao.superafflatus;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Auther ChaooGG
 * @Date 2021-11-22 18:21
 */
public class LockDemo {
    public static void main(String[] args) {




        LTicket lTicket = new LTicket();
        new Thread(()->{
            for (int i=0;i<40;i++){
                lTicket.sale();
            }
        },"AAA").start();

        new Thread(()->{
            for (int i=0;i<40;i++){
                lTicket.sale();
            }
        },"BBB").start();

        new Thread(()->{
            for (int i=0;i<40;i++){
                lTicket.sale();
            }
        },"CCC").start();

    }


}



class LTicket{
    private  int number =30;

    ReentrantLock lock =   new   ReentrantLock();


    void sale(){
        try {
            lock.lock();

            if(number>0){
                System.out.println(Thread.currentThread().getName()+"该线程卖出"+ number -- +"还剩下"+number+"张");
            }
        }
         catch(Exception e){
             System.out.println(e.getMessage());

        }finally {
            lock.unlock();
        }

    }
}